package com.blb.web.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;

/**
 * 提供基础servlet服务
 * 1、跳转页面
 * 2、返回json
 * 3、重定向
 * 4、分页处理
 * 5、请求映射
 * 
 * @author Administrator
 *
 */
public class BaseServlet extends HttpServlet{
	
	private static final long serialVersionUID = -4652027379832527867L;

	private static final String JSP_SUFFIX = ".jsp";
	
	private int pageNo = 1;
	
	private int pageSize = 10;
	
	private HttpServletRequest request;
	
	private HttpServletResponse response;
	
	/**
	 * 转发页面
	 * @param request
	 * @param response
	 * @param url
	 */
	public void response(String url) {
		try {
			String toUrl = url + JSP_SUFFIX;
			System.out.println("跳转到：" + toUrl);
			request.getRequestDispatcher("/" + toUrl).forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 返回json
	 * @param response
	 * @param context
	 * @return 
	 * @throws IOException
	 */
	public void responseJson(String context) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter() ;
		out.write(context);
		out.close();
	}
	
	/**
	 * 重写向页面
	 * @param response
	 * @param url
	 * @throws IOException
	 */
	public void responseRedirect(String url) throws IOException {
		response.sendRedirect(url);
	}

	/**
	 * 将请求映射到相应方法，使用servlet也可以像springMVC一样
	 * @param request
	 * @param response
	 * @param clazz
	 * @throws ServletException
	 * @throws IOException
	 */
	public void forward(HttpServletRequest request, HttpServletResponse response, Class<?> clazz) throws ServletException, IOException {
		this.request = request;
		this.response = response;
		// 获取请求的URI地址信息
        String url = request.getRequestURI();
        // 截取其中的方法名
        String methodName = url.substring(url.lastIndexOf("/")+1, url.length());
        Method method = null;
        try {
            // 使用反射机制获取在本类中声明了的方法
            method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
        	e.printStackTrace();
        	response("404");
        }
        try {
        	// 执行方法
			method.invoke(this, request, response);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
			response("500");
		}
	}
	
	/**
	 * 获取参数，为空则抛异常
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public String getNotNullParam(String name) throws Exception {
		String parameter = request.getParameter(name);
		if (StringUtils.isNullOrEmpty(parameter)) {
			throw new Exception(name + "不能为空！！！");
		}
		return parameter;
	}
	
	/**
	 * 获取参数，为空不做处理
	 * @param name
	 * @return
	 */
	public String getParam(String name) {
		return request.getParameter(name);
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		if (!StringUtils.isNullOrEmpty(pageNo)) {
			this.pageNo = (Integer.parseInt(pageNo)-1) * pageSize;
		}else {
			this.pageNo = 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		if (!StringUtils.isNullOrEmpty(pageSize)) {
			this.pageSize = Integer.parseInt(pageSize);
		}
	}
	
	
}
